# -*- coding: utf-8 -*-
# @Time: 2021/8/20 8:38
# @File: arg-28.py
# @DESC:
import re


# 找到开头相同的字段做比较，时间消耗太大了
def func1(haystack, needle):
    if len(needle) == 0:
        return 0
    for index, i in enumerate(haystack):
        if i == needle[0] and len(haystack) - index >= len(needle):
            temp = 0
            for index2, j in enumerate(needle):
                if haystack[index + index2] == j:
                    temp += 1
                else:
                    break
            if temp == len(needle):
                return index

    return -1


def func2(haystack, needle):
    if len(needle) == 0:
        return 0
    for index, i in enumerate(haystack):
        if i == needle[0] and haystack[index:index + len(needle)] == needle:
            return index
    return -1


def func3(haystack, needle):
    if len(needle) == 0:
        return 0
    lent1 = len(haystack)
    lent2 = len(needle)
    for i in range(lent1 - lent2 + 1):
        temp = 0
        for j in range(lent2):
            if needle[j] == haystack[i + j]:
                temp += 1
            else:
                break
        if temp == lent2:
            return i

    return -1


haystack = "aaaaa"
needle = "bba"
r = func3(haystack, needle)
print(r)
